package com.blueocean_health.care.management.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blueocean_health.care.management.common.LocalManager;
import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.domain.base.NurseGroupPercentages;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.User;
import com.blueocean_health.care.management.domain.dto.NurseGroupPercentagesDto;
import com.blueocean_health.care.management.em.LocalManagerType;
import com.blueocean_health.care.management.em.NurseHavePercentagesEnum;
import com.blueocean_health.care.management.mapper.NurseGroupPercentagesMapper;
import com.blueocean_health.care.management.service.NurseGroupPercentagesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 护工小组提成管理  2018年5月14日18:41:52 
 * @author wuyanli
 *
 */
@Service
public class NurseGroupPercentagesServiceImpl implements NurseGroupPercentagesService{
	
	@Autowired
	private NurseGroupPercentagesMapper percentDao;
	
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
	
	/**
	 * 分页查询分页列表  2018年5月16日14:47:02
	 */
	@Override
	public PageInfo<NurseGroupPercentagesDto> percentagesList(Map<String, String> map) {
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("hospitalId", user.getHospitalId());
		int pageSize = Integer.parseInt(map.get("pageSize"));
		int page = Integer.parseInt(map.get("page"));
		PageHelper.startPage(page,pageSize);
		List<NurseGroupPercentagesDto> percentagesList = percentDao.percentagesList(map);
		for (NurseGroupPercentagesDto nurseGroupPercentagesDto : percentagesList) {
			nurseGroupPercentagesDto.setHavePercentagesName(NurseHavePercentagesEnum
					.valueOf("_"+nurseGroupPercentagesDto.getHavePercentages()).getVal());
			if(NurseHavePercentagesEnum._1.getKey().equals(nurseGroupPercentagesDto.getHavePercentages()+"")){
				String stringByDate = "";
				if(!StringUtils.isEmpty(nurseGroupPercentagesDto.getParticipationDate())){
					Date dateByString = TimeUtils.getDateByString(nurseGroupPercentagesDto.getParticipationDate(), TimeType.yyyy_MM_dd_HH_mm_ss);
					stringByDate = TimeUtils.getStringByDate(dateByString, TimeType.yyyy_MM_dd);
					
				}
				nurseGroupPercentagesDto.setParticipationDate(stringByDate);
			}
		}
		PageInfo<NurseGroupPercentagesDto> pageInfo = new PageInfo<NurseGroupPercentagesDto>(percentagesList);
		return pageInfo;
	}

	/**
	 * 编辑护工小组提成信息 第一编辑为新增 其他编辑为修改  2018年5月14日18:42:50
	 */
	@Override
	public SysResult modifyGroupPercentages(NurseGroupPercentages groupPercentages) {
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		groupPercentages.setHospitalId(user.getHospitalId());
		Map<String,String> map = new HashMap<>();	
		map.put("nurseId", groupPercentages.getNurseId());
		map.put("hospitalId", user.getHospitalId());
		/**
		 * 根据护工Id查询提成管理表中改护工信息是否存在
		 */
		List<NurseGroupPercentages> percentByNurseId = percentDao.getPercentByNurseId(map);
		//是否参与提成 1：是  0：否
		if("0".equals(groupPercentages.getHavePercentages())){
			//不参与提成 参与日期为空
			groupPercentages.setParticipationDate(null);
		}
		int insertGroupPercentages = 0;
		if(StringUtils.isEmpty(percentByNurseId) || percentByNurseId.size() == 0){
			// 新增操作
			insertGroupPercentages = percentDao.insertGroupPercentages(groupPercentages);
		}else{
			//修改操作
			insertGroupPercentages = percentDao.updateGroupPercentages(groupPercentages);
		}
		if(insertGroupPercentages <= 0){
			return SysResult.error();
		}
		return SysResult.ok();
	}

	@Override
	public List<NurseGroupPercentagesDto> percentagesListExcel(Map<String, String> map) {
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("hospitalId", user.getHospitalId());
		
		List<NurseGroupPercentagesDto> percentagesList = percentDao.percentagesList(map);
		for (NurseGroupPercentagesDto nurseGroupPercentagesDto : percentagesList) {
			nurseGroupPercentagesDto.setHavePercentagesName(NurseHavePercentagesEnum
					.valueOf("_"+nurseGroupPercentagesDto.getHavePercentages()).getVal());
			if(NurseHavePercentagesEnum._1.getKey().equals(nurseGroupPercentagesDto.getHavePercentages()+"")){
				String stringByDate = "";
				if(!StringUtils.isEmpty(nurseGroupPercentagesDto.getParticipationDate())){
					Date dateByString = TimeUtils.getDateByString(nurseGroupPercentagesDto.getParticipationDate(), TimeType.yyyy_MM_dd_HH_mm_ss);
					stringByDate = TimeUtils.getStringByDate(dateByString, TimeType.yyyy_MM_dd);
					
				}
				nurseGroupPercentagesDto.setParticipationDate(stringByDate);
			}
		}
		
		return percentagesList;
	}

	
}
